package cn.itsource.ymcc.producer;

import cn.itsource.ymcc.constant.OrderConstants;
import cn.itsource.ymcc.constant.RConst;
import cn.itsource.ymcc.domain.CourseOrder;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OrderProducerImpl implements OrderProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;



    @Override
    public void sendSystemMessage(String message) {
        rocketMQTemplate.sendOneWay(OrderConstants.TOPIC_ORDER_SYSTEM_MESSAGE_NAME+":"+OrderConstants.TAGS_ORDER_SYSTEM_MESSAGE_NAME,
                MessageBuilder.withPayload(message).build());
    }

    @Override
    public void sendTransactionMessage(CourseOrder courseOrder) {
        rocketMQTemplate.sendMessageInTransaction(OrderConstants.MQ_COURSEORDER_PAY_GROUP_TRANSACTION,
                OrderConstants.MQ_TOPIC_COURSEORDER_PAYORDER+ RConst.Str.COLON+OrderConstants.MQ_TAGS_COURSEORDER_PAYORDER,
                MessageBuilder.withPayload(JSON.toJSONString(courseOrder)).build(),null);
    }


}